package com.zhentao.recharge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.pojo.TOrder;
import com.zhentao.recharge.mapper.RechargeMapper;
import com.zhentao.recharge.service.RechargeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 充值记录Service实现类
 * 
 * @author zhentao
 * @since 2024-08-13
 */
@Slf4j
@Service
public class RechargeServiceImpl implements RechargeService {

    @Autowired
    private RechargeMapper rechargeMapper;

    @Override
    public IPage<TOrder> getRechargeRecords(Page<TOrder> page, Long patientId, Date startDate, Date endDate, String bizType) {
        log.info("查询充值记录，患者ID：{}，开始日期：{}，结束日期：{}，业务类型：{}", patientId, startDate, endDate, bizType);
        
        try {
            IPage<TOrder> result = rechargeMapper.selectRechargeRecords(page, patientId, startDate, endDate, bizType);
            log.info("查询充值记录成功，共{}条", result.getTotal());
            return result;
        } catch (Exception e) {
            log.error("查询充值记录失败", e);
            throw new RuntimeException("查询充值记录失败", e);
        }
    }

    @Override
    public List<TOrder> getRechargeRecordsByPatientId(Long patientId) {
        log.info("根据患者ID查询充值记录，患者ID：{}", patientId);
        
        try {
            List<TOrder> result = rechargeMapper.selectByPatientId(patientId);
            log.info("根据患者ID查询充值记录成功，共{}条", result.size());
            return result;
        } catch (Exception e) {
            log.error("根据患者ID查询充值记录失败", e);
            throw new RuntimeException("根据患者ID查询充值记录失败", e);
        }
    }

    @Override
    public TOrder getRechargeDetail(Integer orderId) {
        log.info("查询充值记录详情，订单ID：{}", orderId);
        
        try {
            TOrder result = rechargeMapper.selectRechargeDetail(orderId);
            if (result != null) {
                log.info("查询充值记录详情成功");
            } else {
                log.warn("未找到订单ID为{}的充值记录", orderId);
            }
            return result;
        } catch (Exception e) {
            log.error("查询充值记录详情失败", e);
            throw new RuntimeException("查询充值记录详情失败", e);
        }
    }

    @Override
    public boolean addRechargeRecord(TOrder order) {
        log.info("新增充值记录，订单编号：{}", order.getOrderNumber());
        
        try {
            // 设置创建时间
            if (order.getCreateTime() == null) {
                order.setCreateTime(new Date());
            }
            if (order.getOperateTime() == null) {
                order.setOperateTime(new Date());
            }
            
            int result = rechargeMapper.insert(order);
            boolean success = result > 0;
            log.info("新增充值记录{}，订单ID：{}", success ? "成功" : "失败", order.getOrderId());
            return success;
        } catch (Exception e) {
            log.error("新增充值记录失败", e);
            throw new RuntimeException("新增充值记录失败", e);
        }
    }

    @Override
    public boolean updateRechargeRecord(TOrder order) {
        log.info("更新充值记录，订单ID：{}", order.getOrderId());
        
        try {
            int result = rechargeMapper.updateById(order);
            boolean success = result > 0;
            log.info("更新充值记录{}", success ? "成功" : "失败");
            return success;
        } catch (Exception e) {
            log.error("更新充值记录失败", e);
            throw new RuntimeException("更新充值记录失败", e);
        }
    }

    @Override
    public boolean deleteRechargeRecord(Integer orderId) {
        log.info("删除充值记录，订单ID：{}", orderId);
        
        try {
            int result = rechargeMapper.deleteById(orderId);
            boolean success = result > 0;
            log.info("删除充值记录{}", success ? "成功" : "失败");
            return success;
        } catch (Exception e) {
            log.error("删除充值记录失败", e);
            throw new RuntimeException("删除充值记录失败", e);
        }
    }
} 